﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
namespace Yugi_Gui
{
	/// <summary>
	/// Interaction logic for MessageBox.xaml
	/// </summary>
	public partial class AskPositionMessageBox : UserControl
	{
        public event Action<object, string> OnChoice;
        private Canvas parent;
        public AskPositionMessageBox()
		{
			this.InitializeComponent();
            this.State = MState.Hide;
            Linker.ask_pos_message_box = this;
            this.Loaded += new RoutedEventHandler(MyMessageBox_Loaded);
		}

        void MyMessageBox_Loaded(object sender, RoutedEventArgs e)
        {
            //this.parent = this.Parent as Canvas;
            //this.parent.Children.Remove(this);
        }
        Point last= new Point(-999,-999);
        void MyMessageBox_MouseMove(object sender, MouseEventArgs e)
        {
            
            Point p = e.GetPosition(this.Parent as UIElement);
          
          
            if (e.LeftButton == MouseButtonState.Pressed)
            {
               
                double left = Canvas.GetLeft(this);
                double top = Canvas.GetTop(this);
                Canvas.SetLeft(this, left + p.X - last.X);
                Canvas.SetTop(this, top + p.Y - last.Y);
                
            }
            last = p;
        }
        
        bool allow_drag = false;
        public bool AllowDrag
        {
            get
            {
                return allow_drag;
            }
            set
            {
                allow_drag = value;
                if (this.Parent is Canvas)
                {
                    if (value == true)
                        this.MouseMove += new MouseEventHandler(MyMessageBox_MouseMove);
                    else
                        this.MouseMove -= new MouseEventHandler(MyMessageBox_MouseMove);
                }
            }
        }
        public string Message
        {
            get
            {
                return this.message.Text;
            }
            set
            {
                this.message.Text = value;
            }
        }
        private MState current;
        public MState State
        {
            get
            {
                return current;
            }
            set
            {
                current = value;
                VisualStateManager.GoToState(this, current.ToString(), true);
               
            }
        }
        public void ShowMsg(Yugi.Client.Card.YBaseCard card)
        {
            this.img1.Source = card.displayImage;
            this.img2.Source = card.displayImage;
            Canvas.SetZIndex(this, 100);
            this.Visibility = Visibility.Visible;
            this.State = MState.Show;
            ((Canvas)this.Parent).Background = Brushes.Black;
            ((Canvas)this.Parent).Opacity = 0.9;
            ((Canvas)this.Parent).IsHitTestVisible = true;
        }
        public void ShowMsg(double x,double y)
        {
            this.Visibility = Visibility.Visible;
            Canvas.SetLeft(this, x);
            Canvas.SetTop(this, y);
            this.State = MState.Show;
            
        }

        private void img1_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Image bt = sender as Image;
            string res = null;
            switch (bt.DataContext.ToString())
            {
                case "1":
                    res = "UP";
                    break;
                case "2":
                    res = "DOWN";
                    break;
            }
            if (OnChoice != null && res != null)
            {
                OnChoice(this, res);

            }
            Canvas.SetZIndex(this, -100);
            ((Canvas)this.Parent).Background = Brushes.Transparent;
            ((Canvas)this.Parent).Opacity = 1;
            ((Canvas)this.Parent).IsHitTestVisible = false;
            this.State = MState.Hide;
            this.Visibility = Visibility.Collapsed;
            //this.parent.Children.Remove(this);
        }

        private void img1_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image bt = sender as Image;
            switch (bt.DataContext.ToString())
            {
                case "1":
                    img2.Opacity = 0.5;
                    break;
                case "2":
                    img1.Opacity = 0.5;
                    break;
            }
        }

        private void img1_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            img1.Opacity = 1;
            img2.Opacity = 1;
        }
        public enum MState
        {
            Show,Hide
        }
	}
}